<?php

namespace Home\Model;

class TeamDataModel extends CommonModel {

    public function createTeamData($match_id){
        $tm = time();
        $dt = array(
            array(
                'team_id' => $_POST['home_team_id'],
                'match_id' => $match_id,
                'create_time' => $tm,
                'update_time' => $tm,
            ),
            array(
                'team_id' => $_POST['away_team_id'],
                'match_id' => $match_id,
                'create_time' => $tm,
                'update_time' => $tm,
            ),
        );

        foreach ($dt as $k => $v) {
            $rs = $this->add($v);
        }

        if ($rs) {
            $ret['status'] = true;
            $ret['msg'] = '操作成功！';
        } else {
            $ret['status'] = false;
            $ret['msg'] = $this->getError();
        }
        return $ret;

    }

    public function findMatchTeamData($match){
        $dt = array();
        $flt = array(
            'team_id' => $match['home_team_id'],
            'match_id' => $match['match_id'],
        );

        $dt['home_team_data'] = $this->findTeamData($flt);
        $flt = array(
            'team_id' => $match['away_team_id'],
            'match_id' => $match['match_id'],
        );
        $dt['away_team_data'] = $this->findTeamData($flt);
        return $dt;
    }

    public function findTeamData($flt){
        return $this->where($flt)->find($flt);
    }

    public function updateTeamData($get){
        $flt = array(
            'team_id' => $get['team_id'],
            'match_id' => $get['match_id'],
        );
        if ($get['oper'] == 'plus') {
            $rs = $this->where($flt)->setInc($get['type'], 1);
        } else {
            $rs = $this->where($flt)->setDec($get['type'], 1);
        }

        if ($rs) {
            $ret['status'] = true;
            $ret['msg'] = '操作成功！';
        } else {
            $ret['status'] = false;
            $ret['msg'] = $this->getError();
        }
        return $ret;
    }


}
